package com.yiya.ms.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yiya.bean.Company;
import com.yiya.bean.CompanyUserInfo;
import com.yiya.bean.Employee;
import com.yiya.bean.SerCompany;
import com.yiya.bean.SysUser;
import com.yiya.model.CompanyCplex;
import com.yiya.model.CompanyCplexModel;
import com.yiya.service.CompanyService;
import com.yiya.service.CompanyUserInfoService;
import com.yiya.service.EmployeeService;
import com.yiya.utils.HtmlUtil;
import com.yiya.utils.SessionUtils;

@Controller
@RequestMapping("/company")
public class CompanyAction extends BaseAction{
	@SuppressWarnings("unused")
	private final static Logger log= Logger.getLogger(CompanyAction.class);

	@Autowired
	private CompanyService companyService;
	
	@Autowired
	private CompanyUserInfoService companyUserInfoService;
	
	@Autowired
	private EmployeeService employeeService;
	
	@RequestMapping("/list") 
	public ModelAndView list(HttpServletRequest request){
		Map<String,Object>  context = getRootMap();
		return forword("company/company",context); 
	}
	
	@RequestMapping("/dataList") 
	public void  datalist(CompanyCplexModel model,HttpServletRequest request,HttpServletResponse response) throws Exception{
		SysUser user = SessionUtils.getUser(request);
		//int scope = sysRoleService.queryUserViewScope(user.getId());
		if(user.getSuperAdmin()!=1){
			CompanyUserInfo userInfo = companyUserInfoService.queryByUserId(user.getId());
			if(userInfo!=null){
				model.setId(userInfo.getCompanyId());
			}else{
				sendFailureMessage(response, "找不到您在公司的个人信息！");
			}
		}
		
		List<CompanyCplex> list = companyService.queryByList(model);
		int count = companyService.queryByCount(model);
		//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("rows", list);
		jsonMap.put("total", count);
		HtmlUtil.writerJson(response, jsonMap);
	}
	
	@RequestMapping("/save")
	public void save(Company bean,HttpServletRequest request,HttpServletResponse response){
		SysUser user = SessionUtils.getUser(request);
		Date nowTime = new Date();
		if(bean.getId() == null){
			bean.setCreateBy(user.getId());
			bean.setCreateTime(nowTime);
			companyService.add(bean);
		}else{
			bean.setUpdateBy(user.getId());
			bean.setUpdateTime(nowTime);
			companyService.Update(bean);
		}
		sendSuccessMessage(response, "保存成功");
	}
	
	@RequestMapping("/getId")
	public void getId(Integer id,HttpServletResponse response){
		Map<String,Object>  context = getRootMap();
		Company bean = companyService.queryById(id);
		if(bean  == null){
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", bean);
		HtmlUtil.writerJson(response, context);
	}
	
	@RequestMapping("/delete")
	public void delete(Integer[] id,HttpServletRequest request,HttpServletResponse response) throws Exception{
		SysUser user = SessionUtils.getUser(request);
		companyService.delete(id,user);
		sendSuccessMessage(response, "删除成功");
	}
	
	@RequestMapping("/loadCompanyList")
	public void loadCompanyList(CompanyCplexModel model,HttpServletRequest request,HttpServletResponse response) throws Exception{
		SysUser user = SessionUtils.getUser(request);
		if(user.getSuperAdmin()!=1){
			CompanyUserInfo userInfo = companyUserInfoService.queryByUserId(user.getId());
			if(userInfo!=null){
				model.setId(userInfo.getCompanyId());
			}else{
				sendFailureMessage(response, "找不到您在公司的个人信息！");
			}
		}
		List<CompanyCplex> list = companyService.getCompanyList(model);
		HtmlUtil.writerJson(response, list);
	}
	
	@RequestMapping("/loadCompanyListBySerCompany")
	public void loadCompanyListBySerCompany(CompanyCplexModel model,HttpServletRequest request,HttpServletResponse response) throws Exception{
		SysUser user = SessionUtils.getUser(request);
		if(user.getSuperAdmin()!=1){
			Employee employee = employeeService.queryByUserId(user.getId());
			SerCompany serCompany = new SerCompany();
			serCompany.setId(employee.getSerCompanyId());
			model.setSerCompany(serCompany);
		}
		List<CompanyCplex> list = companyService.getCompanyList(model);
		HtmlUtil.writerJson(response, list);
	}
	
	/**
	 * 获取当前用户所属维修公司相关联的单位
	 * @param request
	 * @return
	 */
	@RequestMapping("/list_by_serCompany") 
	public ModelAndView listBySerCompany(HttpServletRequest request){
		Map<String,Object>  context = getRootMap();
		return forword("company/companyList_by_serCompany",context); 
	}
	
	@RequestMapping("/dataList_by_serCompany") 
	public void  datalistBySerCompany(CompanyCplexModel model,HttpServletRequest request,HttpServletResponse response) throws Exception{
		SysUser user = SessionUtils.getUser(request);
		//int scope = sysRoleService.queryUserViewScope(user.getId());
		if(user.getSuperAdmin()!=1){
			Employee employee = employeeService.queryByUserId(user.getId());
			if(employee!=null){
				SerCompany serCompany =new SerCompany();
				serCompany.setId(employee.getSerCompanyId());
				model.setSerCompany(serCompany);
			}else{
				sendFailureMessage(response, "找不到您在公司的个人信息！");
			}
		}
		
		List<CompanyCplex> list = companyService.queryByList(model);
		int count = companyService.queryByCount(model);
		//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("rows", list);
		jsonMap.put("total", count);
		HtmlUtil.writerJson(response, jsonMap);
	}
}
